from rest_framework.serializers import ModelSerializer, ValidationError
from .models import *
from django.contrib.auth.hashers import make_password
import re


class RegisterSer(ModelSerializer):
    class Meta:
        model = User
        fields = '__all__'

    def create(self, validated_data):
        validated_data["password"] = make_password(validated_data["password"])
        user = User.objects.create(**validated_data)
        return user

    def validate_phone(self, value):
        if not re.match('^1[35789]{1}\d{9}$', value):
            raise ValidationError('手机号格式错误')
        return value

